# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv

#=========月底成产成本计算 - 增加分摊方式: 焊点数 =========#    
    
class cost_calc_mrp(osv.osv):
    _inherit = "cost.calc"
    
    def default_get(self, cr, uid, fields, context=None):
        defaults = super(cost_calc_mrp, self).default_get(cr, uid, fields, context=context)
        if defaults.get('cost_alloc_ids'):
            for cost_alloc in defaults.get('cost_alloc_ids'):
                cost_alloc['alloc_method'] = 'solder'
        return defaults  
    
    def _cost_alloc_solder(self, cr, uid, ids, cost_alloc, params, context=None):
        #统计焊点数
        cr.execute('''
                select sum(coalesce(b.solder_cnt,0)*a.product_qty) as alloc_total
                from stock_move a
                join product_product b on a.product_id = b.id
                where a.id in %s 
                ''',(tuple(params.get('calc_move_ids',[])), ))
        row = cr.fetchone()
        alloc_total = 0.0
        if row and row[0]:
            alloc_total = row[0]
        else:
            #无分配数据, 直接返回
            return True
        #更新分配总额
        self.pool.get('cost.alloc.period').write(cr, uid, [cost_alloc.id], {'alloc_total':alloc_total}, context=context)
        #清除旧数据
        self._cost_alloc_clear(cr, uid, cost_alloc, params, context=context)
        #生成产品分配明细
        cr.execute('''
            insert into cost_alloc_period_prod
            (cost_alloc_period_id, cost_calc_id, product_id, alloc_prod, amount_prod, create_date, create_uid)
            select %s, %s, a.product_id, 
                sum(coalesce(b.solder_cnt,0)*a.product_qty) as alloc_prod, 
                %s*sum(coalesce(b.solder_cnt,0)*a.product_qty)/%s, now(), %s
            from stock_move a
            join product_product b on a.product_id = b.id
            where a.id in %s
            group by a.product_id        
        ''', (cost_alloc.id, cost_alloc.cost_calc_id.id, cost_alloc.amount, alloc_total, uid, tuple(params.get('calc_move_ids',[]))))
        return True  
    
class cost_alloc_period(osv.osv):
    _inherit = 'cost.alloc.period'
    _columns = {
        'alloc_method': fields.selection([('workcost','加工成本'),('solder','焊点数')], '分摊方式', required=True),
        }    
    
    _defaults = {'alloc_method':'solder'}          
        
class cost_alloc_period_prod(osv.osv): 
    _inherit = 'cost.alloc.period.prod'
    _columns = {
        'alloc_method': fields.related('cost_alloc_period_id','alloc_method',type='selection', selection=[('workcost','加工成本'),('solder','焊点数')],store=True, string='分摊方式'),
    }
    _defaults = {'alloc_method':'solder'}
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
